﻿using System;
using System.Collections.Generic;


namespace DailyFx
{
	public static class CollectionExtension
	{
		/// <summary>
		///		Applies the action to each element in the collecton.
		/// </summary>
		public static void Apply<T> ( this IEnumerable<T> enumerable, Action<T> action )
		{
			foreach ( var element in enumerable )
			{
				action ( element );
			}
		}

#if WINDOWS_PHONE && !WP8
		//Method missing in WP7.1 Linq

		/// <summary>
		///		Merges two sequences by using the specified predicate function.
		/// </summary>
		public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult> ( 
			this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
		{
			if (first == null) {
				throw new ArgumentNullException("first");
            }

			if (second == null) {
				throw new ArgumentNullException("second");
            }

			if (resultSelector == null) {
				throw new ArgumentNullException("resultSelector");
            }

			var enumFirst = first.GetEnumerator();
			var enumSecond = second.GetEnumerator();

			while (enumFirst.MoveNext() && enumSecond.MoveNext()) {
				yield return resultSelector(enumFirst.Current, enumSecond.Current);
			}
		}
#endif

	}
}
